package com.tyut.controller;

import com.tyut.pojo.Property;
import com.tyut.service.PropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class PropertyController {

    @Autowired
    private PropertyService propertyService;

    @RequestMapping("/propertyFind")
    protected String propertyFind(Property property, Model model) {

        String building = property.getBuilding();
        String floor = property.getFloor();
        String dorm = property.getDorm();
        String bed = property.getBed();
        List<Property> propertyByBuildingFloorDormBed = propertyService.findPropertyByBuildingFloorDormBed(building, floor, dorm, bed);
        model.addAttribute("list", propertyByBuildingFloorDormBed);
        return "propertymanage";
    }

    @RequestMapping("/listProperty")
    public String listProperty(Model model){
        List<Property> propertyAll = propertyService.findPropertyAll();
        model.addAttribute("list",propertyAll);
        return "propertymanage";
    }

    @RequestMapping("/propertyAdd")
    public String propertyAdd(Property property, Model model) {
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            property.setNtime(df.format(new Date()));
            propertyService.insertProperty(property);
            return "redirect:listProperty";
        }catch(Exception e) {
            e.printStackTrace();
            model.addAttribute("msg", "登记出现错误");
            return "failed";
        }
    }

    @RequestMapping("/propertyAlter")
    public String propertyAlter(HttpServletRequest request) {

        String id = request.getParameter("id");
        propertyService.updateProperty(id);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = df.format(new Date());
        propertyService.updateTime(format,id);

        return "redirect:listProperty";

    }

    @RequestMapping("/propertyAddJSP")
    public String propertyAddJSP(){
        return "propertyadd";
    }

}
